package cn.wanda.bi.sunshine.platform.server.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import cn.wanda.bi.sunshine.platform.server.controller.response.SysAppGroupResponse;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import cn.wanda.bi.sunshine.platform.server.security.bean.mapper.ResourceMenuRelaMapper;
import cn.wanda.bi.sunshine.platform.server.security.bean.mapper.RoleResourceRelaMapper;
import cn.wanda.bi.sunshine.platform.server.security.bean.mapper.SysAppGroupMapper;
import cn.wanda.bi.sunshine.platform.server.security.bean.mapper.SysAppMapper;
import cn.wanda.bi.sunshine.platform.server.security.bean.model.ResourceMenuRela;
import cn.wanda.bi.sunshine.platform.server.security.bean.model.ResourceMenuRelaExample;
import cn.wanda.bi.sunshine.platform.server.security.bean.model.RoleResourceRela;
import cn.wanda.bi.sunshine.platform.server.security.bean.model.RoleResourceRelaExample;
import cn.wanda.bi.sunshine.platform.server.security.bean.model.SysApp;
import cn.wanda.bi.sunshine.platform.server.security.bean.model.SysAppExample;
import cn.wanda.bi.sunshine.platform.server.security.bean.model.SysAppGroup;
import cn.wanda.bi.sunshine.platform.server.security.bean.model.SysAppGroupExample;

/**
 * Created by James on 17/7/11.
 */
@Service
public class MenuPermissionsService {

    @Autowired
    private RoleResourceRelaMapper roleResourceRelaMapper;

    @Autowired
    private ResourceMenuRelaMapper resourceMenuRelaMapper;

    @Autowired
    private SysAppMapper sysAppMapper;

    @Autowired
    private SysAppGroupMapper sysAppGroupMapper;

    @Cacheable(value = "cache:menupermissions", key = "'cache:menupermissions:roleid:'+#roleId")
    public SimpleResponse<List<SysAppGroupResponse>> getResourceList(Integer roleId) {
        RoleResourceRelaExample roleResourceRelaExample = new RoleResourceRelaExample();
        RoleResourceRelaExample.Criteria criteria = roleResourceRelaExample.createCriteria();
        if (roleId != null && StringUtils.isNotEmpty(roleId + ""))
            criteria.andRoleIdEqualTo(roleId);
        List<RoleResourceRela> roleResourceRelas = roleResourceRelaMapper.selectByExample(roleResourceRelaExample);
        if (CollectionUtils.isEmpty(roleResourceRelas))
            return SimpleResponse.success();
        ResourceMenuRelaExample resourceMenuRelaExample = new ResourceMenuRelaExample();
        roleResourceRelas.forEach(p -> {
            resourceMenuRelaExample.or().andResourceIdEqualTo(p.getResourceId());
        });
        List<ResourceMenuRela> resourceMenuRelas = resourceMenuRelaMapper.selectByExample(resourceMenuRelaExample);

        SysAppGroupExample sysAppGroupExample = new SysAppGroupExample();
        sysAppGroupExample.createCriteria().andGroupStateEqualTo("1");
        List<SysAppGroup> sysAppGroups = sysAppGroupMapper.selectByExample(sysAppGroupExample);

        List<SysAppGroupResponse> sysAppGroupResponses = new ArrayList<>();
        sysAppGroups.forEach(p -> {
            SysAppGroupResponse sysAppGroupResponse = new SysAppGroupResponse();
            sysAppGroupResponse.setSysAppGroup(p);

            SysAppExample sysAppExample = new SysAppExample();
            SysAppExample.Criteria criteria1 = sysAppExample.createCriteria();
            criteria1.andAppStateEqualTo("1");
            criteria1.andGroupIdEqualTo(p.getGroupId());
            List<Integer> appIds = new ArrayList<>();
            resourceMenuRelas.forEach(r -> {
                appIds.add(r.getAppId());
            });
            criteria1.andAppIdIn(appIds);
            sysAppExample.setOrderByClause("App_Order asc");
            List<SysApp> sysApps = sysAppMapper.selectByExample(sysAppExample);
            sysAppGroupResponse.setSysApps(sysApps);

            sysAppGroupResponses.add(sysAppGroupResponse);
        });

        List<SysAppGroupResponse> results = new ArrayList<>();
        sysAppGroupResponses.forEach(p -> {
            if (p.getSysApps() != null && p.getSysApps().size() > 0)
                results.add(p);
        });

        return SimpleResponse.success(results);

    }

}
